package com.wuyou.designPatterns.proxy;

/**
 * Printer的代理
 *
 * @Date 2024/10/18 14:41
 * @Author 无忧
 */
public class PrinterProxy implements Printable{

    private String name;
    private Printer real; // 本人，被代理类

    public PrinterProxy(){}
    public PrinterProxy(String name){
        this.name = name;
    }

    @Override
    public synchronized void setPrintName(String name) {
        if (real != null) {
            real.setPrintName(name);
        }
        this.name = name;
    }

    @Override
    public String getPrinterName() {
        return name;
    }

    @Override
    public void print(String string) {
        realize();
        real.print(string);
    }

    private synchronized void realize(){
        if(real == null){
            real = new Printer(name);
        }
    }
}
